﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 学生成绩管理系统
{
    public partial class FrmCourseInfoCx : Form
    {
        public FrmCourseInfoCx(string flag)
        {
            InitializeComponent();
            if (flag == "mod")
            {//修改按钮显示
                this.btnMod.Visible = true;
            }
            else if (flag == "del")
            {//删除按钮显示
                this.btnDel.Visible = true;
            }
        }
        public void bindDataGrid()
        {
            string kcid = this.txtKcid.Text.Trim();
            string kcname = this.txtKcname.Text.Trim();
            string coursetype = (string)this.combCoursetype.SelectedItem;
            string credit = this.txtCredit.Text.Trim();
            CourseInfoData data = new CourseInfoData();
            data.Kcid = kcid;
            data.Kcname = kcname;
            data.Coursetype = coursetype;
            data.Credit = credit;

            try
            {
                DataSet ds = CourseInfoOperation.getCourseInfo(data);
                this.dataGrid1.DataSource = ds.Tables[0];
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            bindDataGrid();
        }

        private void FrmCourseInfoCx_Load(object sender, EventArgs e)
        {
            string ConnectionSttring = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
            SqlConnection cn = new SqlConnection(ConnectionSttring);
            cn.Open();
            string str = "select 课程类型 from 课程信息表 group by 课程类型";
            SqlCommand cm = new SqlCommand(str, cn);
            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                this.combCoursetype.Items.Add(dr[0]);
            }
            cm.Dispose();
            cn.Close();
            cn.Dispose();
        }

        private void btnMod_Click(object sender, EventArgs e)
        {
            if (this.dataGrid1.RowCount <= 0)
            {
                MessageBox.Show("请查询要修改的记录！", "提示");
                return;
            }
            int index = this.dataGrid1.CurrentRow.Index;
            if (index < 0)
            {//必须选择要修改的记录
                MessageBox.Show("请选择要修改的记录！", "提示");
                return;
            }
            else
            {
                //int index = this.dataGrid1.CurrentRow.Index;
                string kcid = this.txtKcid.Text.Trim();
                string kcname = this.txtKcname.Text.Trim();
                string coursetype = (string)this.combCoursetype.SelectedItem;
                string credit = this.txtCredit.Text.Trim();
                CourseInfoData data = new CourseInfoData();
                data.Kcid = kcid;
                data.Kcname = kcname;
                data.Coursetype = coursetype;
                data.Credit = credit;
                DataSet ds = CourseInfoOperation.getCourseInfo(data);
                this.dataGrid1.DataSource = ds.Tables[0];
                FrmaddCourseInfo modcourse = new FrmaddCourseInfo(ds.Tables[0].Rows[index]["课程编号"].ToString());
                modcourse.ShowDialog(this);
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (this.dataGrid1.RowCount <= 0)
            {
                MessageBox.Show("请查询要修改的记录！", "提示");
                return;
            }
            int index = this.dataGrid1.CurrentRow.Index;
            if (index < 0)
            {//必须选择要删除的记录
                MessageBox.Show("请选择要删除的记录", "提示");
                return;
            }
            else
            {
                if (MessageBox.Show("确认要删除吗？", "删除", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    string kcid = this.txtKcid.Text.Trim();
                    string kcname = this.txtKcname.Text.Trim();
                    string coursetype = (string)this.combCoursetype.SelectedItem;
                    string credit = this.txtCredit.Text.Trim();
                    CourseInfoData data = new CourseInfoData();
                    data.Kcid = kcid;
                    data.Kcname = kcname;
                    data.Coursetype = coursetype;
                    data.Credit = credit;
                    DataSet ds = CourseInfoOperation.getCourseInfo(data);
                    string id = ds.Tables[0].Rows[index]["课程编号"].ToString();
                    try
                    {
                        if (CourseInfoOperation.deleteCourseInfo(id))
                        {
                            MessageBox.Show("删除成功！", "提示");
                            bindDataGrid();
                        }
                        else
                        {
                            MessageBox.Show("删除失败！", "错误");
                        }
                    }
                    catch (Exception ex)
                    {
                        ex.ToString();
                        MessageBox.Show("删除失败！", "错误");
                    }
                }
            }
        }
    }

}